package com.feicheng.lanniao.service.impl;

import com.feicheng.lanniao.entity.Client;
import com.feicheng.lanniao.entity.Person;
import com.feicheng.lanniao.exception.SystemException;
import com.feicheng.lanniao.mapper.ClientMapper;
import com.feicheng.lanniao.mapper.PersonMapper;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.IClientService;
import com.feicheng.lanniao.service.IPersonService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-09-02
 */
@Service
public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client> implements IClientService {
	@Autowired
	private ClientMapper clientMapper;
	@Autowired
	private PersonMapper personMapper;
	@Autowired
	HttpSession session;
	@Autowired
	QuotaRecordServiceImpl quotaRecordService;
	/**
	 * @see IClientService.addclienInfo
	 */
	@Override
	public ResultInfo addclienInfo(Client client) {	
		//如果返回值为1则返回success
		if(clientMapper.insert(client) > 0 ) {
			return ResultInfo.success();
		} else {
			//否则返回error
			return ResultInfo.error("添加有误");
		}
	}
	/**
	 * @see IPersonService.deleteClient
	 */
	@Override
	public ResultInfo deleteClient(Integer clientId) {
		QueryWrapper<Client> queryWrapper = new QueryWrapper<Client>();
		//判断id相同
		queryWrapper.eq("client_id",clientId);
		Client client = clientMapper.selectOne(queryWrapper);
		UpdateWrapper<Client> updateWrapper = new UpdateWrapper<Client>();
		//根据id修改delete_flag达到假删除效果
		updateWrapper.eq("client_id", clientId);
		if(client.getDeleteFlag() == true) {
			client.setDeleteFlag(false);
		}else {
			client.setDeleteFlag(true);
		}
		clientMapper.update(client, updateWrapper);
		return ResultInfo.success();
	}
	/**
	 * @see IClientService.updateClient
	 */
	@Override
	public ResultInfo updateClient(Client client) {
		QueryWrapper<Client> cQueryWrapper = new QueryWrapper<Client>();
		//根据id修改对应信息
		cQueryWrapper.eq("client_id",client.getClientId());
		if(null == client.getClientId()) {	
		}
		clientMapper.update(client, cQueryWrapper);
		return ResultInfo.success();
	}	
	/**
	 * @see IClientService.selectOne
	 */
	@Override
	public ResultInfo selectOne(Integer clientId) {
		QueryWrapper<Client> queryWrapper = new QueryWrapper<Client>();
		queryWrapper.eq("client_id", clientId);
		return ResultInfo.success(clientMapper.selectOne(queryWrapper));
	}	
	/**
	 * @see IClientService.login
	 */
	@Override
	public ResultInfo login(Client client) {
		if (StringUtils.isEmpty(client.getAccount())) {
			//判断账号是否为空
			throw new SystemException("账户名必须输入");
		}
		if(StringUtils.isEmpty(client.getPassword())) {
			//判断密码是否为空
			throw new SystemException("密码必须输入");
		}
			//从数据库查账号是否存在
		QueryWrapper<Client> queryClient = new QueryWrapper<Client>();
		queryClient.eq("account", client.getAccount());
		List<Client> resulList = list(queryClient);
			//当返回的值为0时
		if (resulList == null || resulList.size() == 0 ) {
			throw new SystemException("账户不存在");
			//获取数据大于1时
		} else if (resulList.size() > 1) {
			throw new SystemException("数据异常，系统将在30秒后冻结相关资源，请联系管理员。");
		}
			//获取数据等于1时,装载到对象中
		Client currentClient = resulList.get(0);			
			//密码检查
		//变量“currentClient].密码和输入“用户信息Bean.密码”不相同时
		if (!currentClient.getPassword().equals(client.getPassword())) {
			throw new SystemException("密码错误");
			}
		//登陆后处理
		//初始化 CURRENT_CLIENT_INFO (MAP)
		JSONObject CURRENT_CLIENT_INFO = new JSONObject();
		//currentClient设定为Null
		currentClient.setPassword(null);
		//放入键 值
		CURRENT_CLIENT_INFO.put("client", currentClient);
		session.setAttribute("CURRENT", CURRENT_CLIENT_INFO);
		return ResultInfo.success(CURRENT_CLIENT_INFO);
	}
	/**
	 * @see IClientService.register
	 */
	public ResultInfo register(Client client) {
		if (StringUtils.isEmpty(client.getAccount())) {
			//判断账号是否为空
			throw new SystemException("账户名必须输入");
		}
		if(StringUtils.isEmpty(client.getPassword())) {
			//判断密码是否为空
			throw new SystemException("密码必须输入");
		}
			//从数据库查账号是否存在
		QueryWrapper<Client> queryClient = new QueryWrapper<Client>();
		queryClient.eq("account", client.getAccount());
		List<Client> resulList = list(queryClient);		
			//获取数据大于等于1时
		 if (resulList.size() >= 1) {
			throw new SystemException("账户已存在");
		}
		this.save(client);
		 QueryWrapper<Client> clientQueryWrapper = new QueryWrapper<>();
		Client client1 = this.getOne(clientQueryWrapper.eq("account",client.getAccount()));
		quotaRecordService.initQuota(client1.getClientId());
		//添加session
		//初始化 CURRENT_CLIENT_INFO (MAP)
		 
		 //添加客户
			return ResultInfo.success(client);
	}
}
